package com.yy.controller.service.Impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.yy.context.BaseContext;
import com.yy.controller.mapper.EmployeeMapper;
import com.yy.controller.service.EmployeeService;
import com.yy.dto.EmployeePageQueryDTO;
import com.yy.entity.Employee;
import com.yy.result.PageResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Slf4j
@Service
public class EmployeeServiceImpl implements EmployeeService {
   @Autowired
    private  EmployeeMapper employeeMapper;
    /**
     * 分页查询员工列表
     * @param employeePageQueryDto
     * @return
     */
    @Override
    public PageResult getPageList(EmployeePageQueryDTO employeePageQueryDto) {
        PageHelper.startPage(employeePageQueryDto.getPage(), employeePageQueryDto.getPageSize());
        Page<Employee> page= employeeMapper.getPageList(employeePageQueryDto);

        System.out.println("分页查询:"+page);
        long total = page.getTotal();
        List<Employee> records = page.getResult();
        return new PageResult(total, records);
    }

    /**
     * 根据id删除员工
     * @param id
     */
    @Override
    public void deleteEmployee(Long id) {
        employeeMapper.deleteEmployee(id);
    }

    /**
     * 新增员工
     * @param employee
     */
    @Override
    public void addEmployee(Employee employee) {
        //获取操作人
        Long userId = BaseContext.getCurrentId();
        employee.setCreateUser(userId);
        employee.setUpdateUser(userId);
        employee.setCreateTime(LocalDateTime.now());
        employee.setUpdateTime(LocalDateTime.now());
        //调用mapper
        employeeMapper.addEmployee(employee);
    }

    /**
     * 更新员工信息
     * @param employee
     */
    @Override
    public void updateEmployee(Employee employee) {
        //获取操作人
        Long userId = BaseContext.getCurrentId();
        employee.setUpdateUser(userId);
        employee.setUpdateTime(LocalDateTime.now());
        employeeMapper.updateEmployee(employee);
    }
}
